//一个APP可以拥有多个控制器，而一个控制器专门来控制某个范围视图，负责这个视图的各种渲染操作。
angular.module("proApp",[])
    .controller("proctrl",["$http",function($http){
        var pc = this;
        pc.showRegBtn = true;
        pc.showUpdateBtn = false;

        pc.modelTitle = "新增商品信息";

        //项目名称
        pc.title = "EShop商品管理";

        pc.f_catalog = [
            {
                firstCatelogId:1,
                firstCatelogName:"蔬菜"},
            {
                firstCatelogId:2,
                firstCatelogName:"服装"},
            {
                firstCatelogId:3,
                firstCatelogName:"二次元"}
        ];

        pc.s_catalog = [
            {
                iD:1,
                name:"豆类",
                firstId:1},
            {
                iD:2,
                name:"树冠类",
                firstId:1},
            {
                iD:3,
                name:"菌类",
                firstId:1},
            {
                iD:4,
                name:"T恤",
                firstId:2},
            {
                iD:5,
                name:"衬衫",
                firstId:2},
            {
                iD:6,
                name:"秋裤",
                firstId:2},
            {
                iD:7,
                name:"Lolita",
                firstId:3},
            {
                iD:8,
                name:"荣耀",
                firstId:3},
            {
                iD:9,
                name:"飞升",
                firstId:3},
        ];
        //商品列表数据
        pc.proInfos = [
            {   id:"1001",
                name:"大西瓜",
                image:"#",
                commercialPrice:232.00,
                marketPrice:300.00,
                remark:"夏季解暑必备",
                stockQty:3000,
                purchaseQty:0,
                secondCategorieID:1,
                catalog:"秋裤",
                creatTime:"2015-6-12"},
            {
                id:"1002",
                name:"大瓜",
                image:"#",
                commercialPrice:2312.00,
                marketPrice:3020.00,
                remark:"夏季解暑必备",
                stockQty:30030,
                purchaseQty:0,
                secondCategorieID:3,
                catalog:"秋裤",
                creatTime:"2016-6-12"}
        ];

        //新登记的商品数据
        pc.newpro = {
            // 序号
            // 商品名称
            // 展示图片
            // 现在价格
            // 市场价格
            // 商品介绍
            // 库存总量
            // 所属分类
            // 创建时间
            id:"1001",
            name:"大西瓜",
            image:"#",
            commercialPrice:232.00,
            marketPrice:300.00,
            remark:"夏季解暑必备",
            stockQty:3000,
            purchaseQty:0,
            secondCategorieID:1,
            catalog:"秋裤",
            creatTime:"2015-6-12"
        };

        $http({
            method:"POST",
            url:"/eshop-project/Commodity/loadAll",
            params:{

            }
        }).then(
            function(resp){
                pc.proInfos = resp.data;
                console.log("success load ok! size="+resp.data.length);

            },
            function(resp){
                console.log("fail to load records!");
            }
        );

        $http({
            method:"POST",
            url:"/eshop-project/firstCatelog/loadAlljson",
            params:{

            }
        }).then(
            function(resp){
                pc.f_catalog = resp.data;
                console.log("success load ok! size="+resp.data.length);
                console.log(pc.f_catalog.Name);

            },
            function(resp){
                console.log("fail to load first_catalog!");
                console.log(pc.f_catalog.Name);
            }
        );

        $http({
            method:"POST",
            url:"/eshop-project/secondCatelog/loadAlljson",
            params:{

            }
        }).then(
            function(resp){
                pc.s_catalog = resp.data;
                console.log("success load ok! size="+resp.data.length);

                
            },
            function(resp){
                console.log("fail to load second_catalog!");
            }
            
        );


        //新增商品登记信息
        pc.addpro= function(scatalogId){
            //       	alert($('#fileUpload'));
//        	var file = $('#fileUpload').files[0];

            pc.newpro.creatTime = getNowFormatDate();
            pc.newpro.secondCategorieID = scatalogId;
            $http({
                method:"POST",
                url:"/eshop-project/Commodity/insert?",
                params:pc.newpro,
//                processData: false,
//                contentType:false
            }).then(
                function(resp){
                    // console.log(resp);
                    // if(resp.data.type==="success"){
                    $('#demoModal').modal('hide');
                    location.reload(true);
                    // }
                    // else{
                    //     alert(resp.message+",重复商品名称是:"+resp.data.name);
                    // }
                },
                function(resp){
                    alert("add car info fail!");
                }
            )
        };

        //车辆信息回填修改
        pc.preUpdatePro = function(idx){

            pc.newpro.id = pc.proInfos[idx].id;
            pc.newpro.image = pc.proInfos[idx].image;
            pc.newpro.commercialPrice = pc.proInfos[idx].commercialPrice;
            pc.newpro.creatTime = pc.proInfos[idx].creatTime;
            pc.newpro.marketPrice = pc.proInfos[idx].marketPrice;
            pc.newpro.name = pc.proInfos[idx].name;
            pc.newpro.purchaseQty = pc.proInfos[idx].purchaseQty;
            pc.newpro.remark = pc.proInfos[idx].remark;
            pc.newpro.secondCategorieID = pc.proInfos[idx].secondCategorieID;
            pc.newpro.stockQty = pc.proInfos[idx].id;

            pc.updatedProIdx = idx;
            pc.showRegBtn = false;
            pc.showUpdateBtn = true;
            pc.modelTitle = "修改商品登记信息";
            $('#demoModal').modal({});
        };

        //车辆信息修改
        pc.updatepro = function(scatalogId){
            pc.newpro.secondCategorieID = scatalogId;
            $http({
                method:"POST",
                url:"/eshop-project/Commodity/update",
                params:pc.newpro
            }).then(
                function(resp){
                    pc.proInfos.splice(pc.updatedProIdx,1,pc.newpro);

                    $('#demoModal').modal('hide');

                    pc.showRegBtn = true;
                    pc.showUpdateBtn = false;

                    pc.newpro = {name:"西瓜"};

                    pc.modelTitle = "新增商品登记信息";

                },
                function(resp){
                    alert(resp.data.message);
                }
            );

        };

        pc.removepro = function(id){

            $http({
                method:"POST",
                url:"/eshop-project/Commodity/delete?",
                params:{
                    commodityId:pc.proInfos[id].id
                }
            }).then(
                function(resp){
                    pc.proInfos.splice(id,1);
                },
                function(resp){
                }
            )
        };



    }]);

//function getpic(){
//	var file = $(#fileUpload).file[0];
//}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + date.getHours() + seperator2 + date.getMinutes()
        + seperator2 + date.getSeconds();
    return currentdate;
}
function sayHello(){
    console.log("javascript hello world!");
}


$(document).ready(function () {

})